home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / CONTRIB / MAN_PC.ZIP / man_pc / whatis / common.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  4.5 KB  |  294 lines

  1. /*
  2.  *    common.c - stuff used by all programs
  3.  */
  4.  
  5. static char *rcsid_common_c = "$Id: common.c,v 2.0 1992/09/13 05:02:44 rosenkra Exp $";
  6.  
  7. /*
  8.  * $Log: common.c,v $
  9.  * Revision 2.0  1992/09/13  05:02:44  rosenkra
  10.  * total rewrite. this if first rev of this file.
  11.  *
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. #include "whatis.h"
  20.  
  21. extern int    debugging;
  22.  
  23. /*------------------------------*/
  24. /*    parse_record        */
  25. /*------------------------------*/
  26.  
  27. /*
  28.  *    macros to make parsing easier
  29.  */
  30. #define EOS            '\0'
  31. #define NL            '\n'
  32. #define FIELDSEP        '%'
  33. #define ITEMSEP            ','
  34.  
  35. #define END_OF_STRING(c)    (c==EOS || c==NL)
  36. #define END_OF_FIELD(c)        (c==FIELDSEP)
  37. #define END_OF_ITEM(c)        (c==ITEMSEP)
  38.  
  39. #define ADVANCE_FIELD    \
  40.  while(!END_OF_STRING(*ps) && !END_OF_FIELD(*ps)){ps++;} \
  41.  if(END_OF_STRING(*ps)){*ps=EOS;return(lastone?0:1);} \
  42.  *ps++ = EOS;
  43.  
  44. #define ADVANCE_ITEM    \
  45.  while(!END_OF_STRING(*ps) && !END_OF_ITEM(*ps) && !END_OF_FIELD(*ps)){ps++;} \
  46.  if(END_OF_STRING(*ps)){*ps=EOS;return(lastone?0:1);} \
  47.  if(END_OF_FIELD(*ps)){*ps++ = EOS;break;} \
  48.  *ps++ = EOS;
  49.  
  50.  
  51. int parse_record (char *raw, struct rec *r)
  52. {
  53.  
  54. /*
  55.  *    parse record. caller provides raw record and space for parsed
  56.  *    structure. ret 0 if all ok, else 1 (incomplete record, etc).
  57.  *    raw input is a null terminated string (with/without newline).
  58.  */
  59.  
  60.     char   *ps = raw;
  61.     int    i;
  62.     int    lastone = 0;
  63.  
  64.  
  65.     /*
  66.      *   set up default (empty)
  67.      */
  68.     r->name     = NULL;
  69.     r->alias[0] = NULL;    r->alias[1] = NULL;
  70.     r->section  = NULL;
  71.     r->subsect  = NULL;
  72.     r->desc     = NULL;
  73.     r->xref[0]  = NULL;    r->xref[1]  = NULL;
  74.     r->keyw[0]  = NULL;    r->keyw[1]  = NULL;
  75.  
  76.  
  77.  
  78.     /*
  79.      *   read name (required!)
  80.      */
  81.     r->name = ps;
  82.     ADVANCE_FIELD;
  83.  
  84.  
  85.  
  86.     /*
  87.      *   read any aliases (optional)
  88.      */
  89.     if (ps[0] == '_' && (END_OF_FIELD(ps[1]) || END_OF_STRING(ps[1])))
  90.     {
  91.         r->alias[0] = NULL;
  92.         ADVANCE_FIELD;
  93.     }
  94.     else
  95.     {
  96.         for (i = 0; i < MAX_ALIAS-1; i++)
  97.         {
  98.             if (*ps == EOS)
  99.             {
  100.                 r->alias[i] = NULL;
  101.                 return (1);
  102.             }
  103.             r->alias[i] = ps;
  104.             r->alias[i+1] = NULL;
  105.             ADVANCE_ITEM;
  106.         }
  107.     }
  108.  
  109.  
  110.  
  111.     /*
  112.      *   read section (required!)
  113.      */
  114.     r->section = ps;
  115.     ADVANCE_FIELD;
  116.  
  117.  
  118.  
  119.     /*
  120.      *   read subsection (optional)
  121.      */
  122.     r->subsect = ps;
  123.     ADVANCE_FIELD;
  124.     if (r->subsect[0] == '_')
  125.         r->subsect = NULL;
  126.  
  127.  
  128.  
  129.     /*
  130.      *   read description (required)
  131.      */
  132.     r->desc = ps;
  133.     ADVANCE_FIELD;
  134.  
  135.  
  136.  
  137.     /*
  138.      *   read any xrefs (optional)
  139.      */
  140.     if (ps[0] == '_' && (END_OF_FIELD(ps[1]) || END_OF_STRING(ps[1])))
  141.     {
  142.         r->xref[0] = NULL;
  143.         ADVANCE_FIELD;
  144.     }
  145.     else
  146.     {
  147.         for (i = 0; i < MAX_XREF-1; i++)
  148.         {
  149.             if (*ps == EOS)
  150.             {
  151.                 r->xref[i] = NULL;
  152.                 return (1);
  153.             }
  154.             r->xref[i] = ps;
  155.             r->xref[i+1] = NULL;
  156.             ADVANCE_ITEM;
  157.         }
  158.     }
  159.  
  160.  
  161.  
  162.     /*
  163.      *   read any keywords (optional)
  164.      */
  165.     lastone = 1;
  166.     if (ps[0] == '_' && (END_OF_FIELD(ps[1]) || END_OF_STRING(ps[1])))
  167.     {
  168.         r->keyw[0] = NULL;
  169.         return (1);
  170.     }
  171.     else
  172.     {
  173.         for (i = 0; i < MAX_KEYW-1; i++)
  174.         {
  175.             if (*ps == EOS)
  176.             {
  177.                 r->keyw[i] = NULL;
  178.                 return (0);
  179.             }
  180.             r->keyw[i] = ps;
  181.             r->keyw[i+1] = NULL;
  182.             ADVANCE_ITEM;
  183.         }
  184.     }
  185.  
  186.  
  187.     return (0);
  188. }
  189.  
  190.  
  191.  
  192.  
  193. /*------------------------------*/
  194. /*    print_record        */
  195. /*------------------------------*/
  196. void print_record (int verbose, struct rec *r)
  197. {
  198.  
  199. /*
  200.  *    print record. one line, just:
  201.  *
  202.  *        name(sect[subsect]) - description.
  203.  *
  204.  *    if verbose, add aliases, see also, and keywords, if any
  205.  */
  206.  
  207.     int i;
  208.  
  209.  
  210.     if (r->name)
  211.         printf ("%s", r->name);
  212.  
  213.  
  214.     if (r->section)
  215.     {
  216.         printf ("(%s", r->section);
  217.         if (r->subsect)
  218.             printf ("%s", r->subsect);
  219.         printf (") -");
  220.     }
  221.     else
  222.         printf (" -");
  223.  
  224.  
  225.     if (r->desc)
  226.         printf (" %s\n", r->desc);
  227.     else
  228.         printf (" unknown (no description in database)\n");
  229.     
  230.  
  231.     if (!verbose)
  232.         return;
  233.  
  234.  
  235.     if (r->alias[0] != NULL)
  236.     {
  237.         printf ("\tprimary manpage:\n");
  238.         for (i = 0; r->alias[i] != NULL; i++)
  239.             printf ("\t\t%s\n", r->alias[i]);
  240.     }
  241.     
  242.     
  243.     if (r->xref[0] != NULL)
  244.     {
  245.         printf ("\tsee also:\n");
  246.         for (i = 0; r->xref[i] != NULL; i++)
  247.             printf ("\t\t%s\n", r->xref[i]);
  248.     }
  249.     
  250.     if (r->keyw[0] != NULL)
  251.     {
  252.         printf ("\tkeywords:\n");
  253.         for (i = 0; r->keyw[i] != NULL; i++)
  254.             printf ("\t\t%s\n", r->keyw[i]);
  255.     }
  256.  
  257.     printf ("\n");
  258.  
  259.     return;
  260. }
  261.  
  262.  
  263.  
  264.  
  265. #ifdef CHECK_MAGIC
  266.  
  267. /*------------------------------*/
  268. /*    check_magic        */
  269. /*------------------------------*/
  270. int check_magic (void)
  271. {
  272.  
  273. /*
  274.  *    check magic "number" of file. if ok, return 0. else 1.
  275.  *    should be first line of file. check only MAGIC_CHECK_LEN bytes.
  276.  */
  277.  
  278.     char    buf[REC_SIZE];
  279.  
  280.     fgets (buf, REC_SIZE-1, stdin);
  281.     if (feof (stdin))
  282.         return (1);
  283.     if (debugging)
  284.         fprintf (stderr, "magic: %s", buf);
  285.  
  286.     if (!strncmp (buf, MAGIC, MAGIC_CHECK_LEN))
  287.         return (0);
  288.  
  289.     return (1);
  290. }
  291. #endif /*CHECK_MAGIC*/
  292.  
  293.  
  294.